﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jmetal.core;
using jmetal.encodings.solutionType;

namespace jmetal.problems
{
  public class Sphere : Problem
  {
    public Sphere (string solutionType, int numberOfVariables)
    {
      // <pex>
      if (numberOfVariables < 0)
        throw new ArgumentException("numberOfVariables < 0", "numberOfVariables");
      // </pex>
      numberOfVariables_ = numberOfVariables;
      numberOfObjectives_ = 1;
      numberOfConstraints_ = 0;
      problemName_ = "Sphere";
      
      lowerLimit_ = new double[numberOfVariables_];
      upperLimit_ = new double[numberOfVariables_];
      for (int i = 0; i < numberOfVariables_; i++) {
        lowerLimit_[i] = -5.12;
        upperLimit_[i] = 5.12;
      }
      solutionType_ = new RealSolutionType (this);
    }

    public override void evaluate (Solution solution)
    {
      // <pex>
      if (solution == (Solution)null)
        throw new ArgumentNullException("solution");
      if (solution.objective_ == (double[])null)
        throw new ArgumentNullException("solution");
      // </pex>
      Variable[] x = solution.variable_;

      Console.WriteLine (solution) ;

      double sum = 0.0;
      for (int var = 0; var < numberOfVariables_; var++) {
        sum += Math.Pow (x[var].value_, 2.0);
      }
      
      solution.objective_[0] = sum;
    }
    // evaluate
  }
}
